//
//  MainViewControllers.m
//  Yooxi_ios
//
//  Created by 达陈 on 15/3/10.
//  Copyright (c) 2015年 达陈. All rights reserved.
//

#import "MainViewControllers.h"
#import "Open_MainViewController.h"
#import "Open_ClassViewController.h"
#import "Open_ShoppingViewController.h"
#import "Open_MyViewController.h"
#import "MainNavigationController.h"

@interface MainViewControllers ()

@end

@implementation MainViewControllers{
    UIImageView *_tabbarView;
    UIImageView *_selectImgView;
    
    //数组
    NSArray *imgNameHight;
    
    NSArray *imgName;
    
    NSMutableArray *arrays;
    
    CGFloat itemWidth;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //2.创建子控制器
    [self _createViewControllers];
    
    //1.创建选项工具栏
    [self _createTabbarView];
    

}

#pragma mark - 1.创建选项工具栏
- (void)_createTabbarView {
    
    //1.遍历取得tabBar中所有的子视图，移除上面内置的按钮
    for(UIView *view in self.tabBar.subviews){
        //用对应的函数取得UITabBarButton 的类对象
        Class cls = NSClassFromString(@"UITabBarButton");
        if([view isKindOfClass:cls]){
            [view removeFromSuperview];
        }
    }
    
    //1.创建横条背景
    _tabbarView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 49)];
    _tabbarView.backgroundColor = UIColorFromRGB(0xF7F7F7);
//    _tabbarView.userInteractionEnabled = YES;
    [self.tabBar addSubview:_tabbarView];
    
    //2.创建按钮
    imgName = @[
                @"9_17",
                @"7_26",
                @"9_20",
                @"9_14",
                ];
    
    imgNameHight = @[
                     @"4_27",
                     @"9_23",
                     @"6_13",
                     @"7_23",
                     ];
    
    itemWidth = kScreenWidth/4.0;
    arrays = [NSMutableArray arrayWithCapacity:4];
    
    for(int i = 0;i<imgName.count;i++){
        UIView *imgviewx = [[UIView alloc] initWithFrame:CGRectMake(itemWidth*i, 0, itemWidth, 49)];
//        imgviewx.userInteractionEnabled = YES;
        imgviewx.backgroundColor = [UIColor clearColor];
        [_tabbarView addSubview:imgviewx];
        
        NSString *name = imgName[i];
        NSString *names = imgNameHight[i];
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 30)];
        if(i == 2){
            button.frame = CGRectMake(0, 0, 30, 30);
        }
        
        button.center = imgviewx.center;
        
        [button setBackgroundImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
        
        
        //设置图片名
        if(i==0){
            [button setBackgroundImage:[UIImage imageNamed:names] forState:UIControlStateNormal];
        }
        
        button.tag = i;
        [button addTarget:self action:@selector(selectTabs:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:button];

        [arrays addObject:button];
    }
    
}


#pragma mark - 2.创建子控制器
- (void)_createViewControllers {
    
    NSMutableArray *tablearray = [NSMutableArray arrayWithCapacity:4];
    
    
    Open_MainViewController *discover = [[Open_MainViewController alloc] initWithNibName:@"Open_MainViewController" bundle:nil];
    discover.view.frame = [[UIScreen mainScreen] bounds];

    
    Open_ClassViewController *all = [[Open_ClassViewController alloc] initWithNibName:@"Open_ClassViewController" bundle:nil];
    all.view.frame = [[UIScreen mainScreen] bounds];
    
    
    Open_ShoppingViewController *collect = [[Open_ShoppingViewController alloc] initWithNibName:@"Open_ShoppingViewController" bundle:nil];
    collect.view.frame = [[UIScreen mainScreen] bounds];
 
    
    Open_MyViewController *myyooxi = [[Open_MyViewController alloc] initWithNibName:@"Open_MyViewController" bundle:nil];
    myyooxi.view.frame = [[UIScreen mainScreen] bounds];
 
    NSArray *array = @[discover,all,collect,myyooxi];
    for (int i=0; i<array.count; i++) {
        UIViewController *viewController = array[i];
        if(i==0){
            [tablearray addObject:viewController];
        }else{
            MainNavigationController *nav = [[MainNavigationController alloc] initWithRootViewController:viewController];
            [tablearray addObject:nav];
        }
    }
    
    
    self.viewControllers = tablearray;
    
}




#pragma mark - button按钮的点击时间

- (void)selectTabs:(UIButton *)button{
    
    
    
    self.selectIndex = button.tag;
    
    //改变标签控制器的控制器试图
    self.selectedIndex = button.tag;

    for (int i=0; i<arrays.count;i++) {
        
        NSString *names = imgNameHight[i];
        NSString *name = imgName[i];
        UIButton *buttonx = arrays[i];
        
        [buttonx setBackgroundImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
        
        if(i == button.tag){
            if (i == 2) {
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                NSString *sucflag = [user objectForKey:@"sucflag"];
                if ([sucflag intValue] == 1) {
                    
                    [buttonx setBackgroundImage:[UIImage imageNamed:names] forState:UIControlStateNormal];
                }
                else
                {
                    NSString *namey = imgNameHight[_selectIndex];
                    UIButton *buttony = arrays[_selectIndex];
                    [buttony setBackgroundImage:[UIImage imageNamed:namey] forState:UIControlStateNormal];
                }
            }
            else
            {
                [buttonx setBackgroundImage:[UIImage imageNamed:names] forState:UIControlStateNormal];
            }
            
            
        }
        
    }
}






@end
